using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class StoreToKitchenEntriesController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();


        public ActionResult Confirm(int id)
        {
            context.Dispose();
            context = new ExpertSMSContext();
            StoreToKitchenEntry storeToKitchenEntry = context.StoreToKitchenEntries.FirstOrDefault(x => x.StoreToKitchenEntryID == id);
            storeToKitchenEntry.IsConfirmed = true;
            foreach (var item in storeToKitchenEntry.StoreToKitchenEntryItems)
            {
                // FIFO Algorithm
                decimal productToCheckOut = item.Quantity / item.Unit.ConvertingFactorToMainUnit;
                foreach (var stock in context.StoreProductStocks.Where(x => x.StoreID == storeToKitchenEntry.StoreID && x.CurrentQuantity > 0).OrderBy(x => x.Date).ToList())
                {
                    if (productToCheckOut > 0)
                    {
                        if (stock.CurrentQuantity >= productToCheckOut)
                        {
                            stock.CurrentQuantity = stock.CurrentQuantity - productToCheckOut;
                            context.SaveChanges();
                            productToCheckOut = 0;
                        }
                        else
                        {
                            productToCheckOut = productToCheckOut - stock.CurrentQuantity;
                            stock.CurrentQuantity = 0;
                            context.SaveChanges();
                        }
                    }
                }
            }
            context.KitchenImportEntries.Add(new KitchenImportEntry
            {
                Date = DateTime.Now,
                IsConfirmed = false,
                StoreToKitchenEntryID = storeToKitchenEntry.StoreToKitchenEntryID
            });
            context.SaveChanges();
            context.Dispose();
            return RedirectToAction("Details", "Stores", new { id = storeToKitchenEntry.StoreID });
        }
        //
        // GET: /StoreToKitchenEntries/

        public ViewResult Index()
        {
            return View(context.StoreToKitchenEntries.Include(storetokitchenentry => storetokitchenentry.Store).Include(storetokitchenentry => storetokitchenentry.Kitchen).Include(storetokitchenentry => storetokitchenentry.StoreToKitchenEntryItems).ToList());
        }

        //
        // GET: /StoreToKitchenEntries/Details/5

        public ViewResult Details(int id)
        {
            StoreToKitchenEntry storetokitchenentry = context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == id);
            return View(storetokitchenentry);
        }

        //
        // GET: /StoreToKitchenEntries/Create

        public ActionResult Create(int storeID)
        {
            ViewBag.Store = context.Stores.FirstOrDefault(x=>x.StoreID == storeID);
            ViewBag.PossibleKitchens = context.Kitchens;
            return View();
        } 

        //
        // POST: /StoreToKitchenEntries/Create

        [HttpPost]
        public ActionResult Create(StoreToKitchenEntry storetokitchenentry)
        {
            if (ModelState.IsValid)
            {
                context.StoreToKitchenEntries.Add(storetokitchenentry);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleKitchens = context.Kitchens;
            return View(storetokitchenentry);
        }
        
        //
        // GET: /StoreToKitchenEntries/Edit/5
 
        public ActionResult Edit(int id)
        {
            StoreToKitchenEntry storetokitchenentry = context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == id);
            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleKitchens = context.Kitchens;
            return View(storetokitchenentry);
        }

        //
        // POST: /StoreToKitchenEntries/Edit/5

        [HttpPost]
        public ActionResult Edit(StoreToKitchenEntry storetokitchenentry)
        {
            if (ModelState.IsValid)
            {
                context.Entry(storetokitchenentry).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleKitchens = context.Kitchens;
            return View(storetokitchenentry);
        }

        //
        // GET: /StoreToKitchenEntries/Delete/5
 
        public ActionResult Delete(int id)
        {
            StoreToKitchenEntry storetokitchenentry = context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == id);
            return View(storetokitchenentry);
        }

        //
        // POST: /StoreToKitchenEntries/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            StoreToKitchenEntry storetokitchenentry = context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == id);
            context.StoreToKitchenEntries.Remove(storetokitchenentry);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}